package com.atguigu.atcrowdfunding.controller;

import com.atguigu.atcrowdfunding.bean.AJAXResult;
import com.atguigu.atcrowdfunding.bean.Page;
import com.atguigu.atcrowdfunding.bean.Role;
import com.atguigu.atcrowdfunding.bean.User;
import com.atguigu.atcrowdfunding.service.RoleService;
import com.atguigu.atcrowdfunding.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * lcd  2020/3/23
 * Description:
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    @RequestMapping("/index")
    public String index(){
        return "user/index";
    }

    @RequestMapping("/add")
    public String add(){
        return "user/add";
    }

    @ResponseBody
    @RequestMapping("/insert")
    public Object insert(User user){
        AJAXResult ajaxResult = new AJAXResult();
        try {
            user.setUserpswd("123456");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            user.setCreatetime(simpleDateFormat.format(new Date()));
            userService.insert(user);
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            e.printStackTrace();
        }
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping("/deletes")
    public Object delete(Integer[] id){
        AJAXResult ajaxResult = new AJAXResult();
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("userIds", id);
            userService.deletes(map);
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            e.printStackTrace();
        }
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping("/delete")
    public Object delete(String id){
        AJAXResult ajaxResult = new AJAXResult();
        try {
            userService.delete(id);
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            e.printStackTrace();
        }
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping("/update")
    public Object update(User user){
        AJAXResult ajaxResult = new AJAXResult();
        try {
            userService.update(user);
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            e.printStackTrace();
        }
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping("/page")
    public Object page(String queryText,Integer pageNo,Integer pageSize){
        AJAXResult ajaxResult = new AJAXResult();
        try {
            Map<String, Object> map = new HashMap<>();
            int offset=(pageNo-1)*pageSize;
            map.put("offset", offset);
            map.put("pagesize", pageSize);
            map.put("queryText", queryText);
            List<User> users=userService.queryPage(map);
            Integer totalRecord=userService.queryCount(map);//查询总记录数
            //需要查出总的页码数,
            int totalpage=(totalRecord%pageSize)==0?totalRecord/pageSize:totalRecord/pageSize+1;
            Page<User> userPage = new Page<>();
            userPage.setList(users);
            userPage.setPageNo(pageNo);
            userPage.setTotalPage(totalpage);
            userPage.setTotalRecord(totalRecord);
            ajaxResult.setDatas(userPage);
            ajaxResult.setSuccess(true);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ajaxResult;
    }

    @RequestMapping("/edit")
    public String goEdit(Integer id,Model model) {
        User user = userService.findById(id);
        model.addAttribute("user", user);
        return "user/edit";
    }

    @RequestMapping("/index2")
    public String index2(@RequestParam(required = false,defaultValue = "1") Integer pageno, @RequestParam(required = false,defaultValue = "2")Integer pagesize, Model model){
        Map<String, Object> map = new HashMap<>();
        map.put("pageno", pageno);
        map.put("pagesize", pagesize);
        List<User> users=userService.queryPage(map);
        Integer totalRecord=userService.queryCount(map);//查询总记录数
        //需要查出总的页码数,
        int totalpage=(totalRecord%pagesize)==0?totalRecord/pagesize:totalRecord/pagesize+1;
        model.addAttribute("users", users);
        model.addAttribute("pageno", pageno);
        model.addAttribute("totalpage", totalpage);
        return "user/index";
    }

    @RequestMapping("/goAssignRole")
    public String goAssignRole(Integer id,Model model) {
        User user = userService.findById(id);
        model.addAttribute("user", user);
        List<Role> roles=roleService.findAll();
        //筛选出已被分配的和未被分配的
        List<Integer> roleIds=userService.findRoleIdsByUserId(id);
        List<Role> assignRoles = new ArrayList<>();
        List<Role> unAssignRoles = new ArrayList<>();
        for (Role role : roles) {
            if(roleIds.contains(role.getId())){
                assignRoles.add(role);
            }else {
                unAssignRoles.add(role);
            }
        }
        model.addAttribute("assignRoles", assignRoles);
        model.addAttribute("unAssignRoles", unAssignRoles);
        return "user/assignRole";
    }

    @ResponseBody
    @RequestMapping("/doAssignRole")
    public Object doAssignRole(Integer[] assignRoleIds,Integer userid){
        AJAXResult ajaxResult = new AJAXResult();
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("roleIds", assignRoleIds);
            map.put("userid", userid);
            userService.doAssignRole(map);
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            e.printStackTrace();
        }
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping("/undoAssignRole")
    public Object undoAssignRole(Integer[] unassignRoleIds,Integer userid){
        AJAXResult ajaxResult = new AJAXResult();
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("roleIds", unassignRoleIds);
            map.put("userid", userid);
            userService.undoAssignRole(map);
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            e.printStackTrace();
        }
        return ajaxResult;
    }
}
